﻿#include "userinputui.h"
#include "ui_userinputui.h"

UserInputUi::UserInputUi(User user, QWidget* parent) : QDialog(parent), ui(new Ui::UserInputUi) {
  ui->setupUi(this);
  this->setWindowTitle("输入密码");

  this->operator<<(user);

  ui->lineEdit_password->setFocus();
  ui->lineEdit_password->setEchoMode(QLineEdit::Password);
  ui->lineEdit_password->setValidator(new QIntValidator(0, 999999));
  ui->lineEdit_password->setPlaceholderText("密码（三位数字组成）");

  QString button_style(
      "QPushButton{border-image: url(:/pic/src/button/key.png);color: white;}"
      "QPushButton:hover{border-image: url(:/pic/src/button/key_hover.png);color: white;}"
      "QPushButton:pressed{border-image: url(:/pic/src/button/key_pressed.png);color: black;}");
  ui->pushButton_ok->setStyleSheet(button_style);
  ui->pushButton_reset->setStyleSheet(button_style);
  ui->pushButton_cancle->setStyleSheet(button_style);
}

UserInputUi::~UserInputUi() { delete ui; }

int UserInputUi::getThePassword() { return ui->lineEdit_password->text().toInt(); }

bool UserInputUi::isOK() { return is_ok; }

const User UserInputUi::getLoginUser() { return login_user; }

UserInputUi& UserInputUi::operator<<(User user) {
  users.insert(user.username(), user);

  ui->comboBox_username->addItem(user.username());

  return *this;
}

void UserInputUi::on_pushButton_num_1_clicked() { ui->lineEdit_password->insert("1"); }

void UserInputUi::on_pushButton_num_2_clicked() { ui->lineEdit_password->insert("2"); }

void UserInputUi::on_pushButton_num_3_clicked() { ui->lineEdit_password->insert("3"); }

void UserInputUi::on_pushButton_num_4_clicked() { ui->lineEdit_password->insert("4"); }

void UserInputUi::on_pushButton_num_5_clicked() { ui->lineEdit_password->insert("5"); }

void UserInputUi::on_pushButton_num_6_clicked() { ui->lineEdit_password->insert("6"); }

void UserInputUi::on_pushButton_num_7_clicked() { ui->lineEdit_password->insert("7"); }

void UserInputUi::on_pushButton_num_8_clicked() { ui->lineEdit_password->insert("8"); }

void UserInputUi::on_pushButton_num_9_clicked() { ui->lineEdit_password->insert("9"); }

void UserInputUi::on_pushButton_reset_clicked() {
  ui->lineEdit_password->clear();
  ui->lineEdit_password->setPlaceholderText("密码（六位数字组成）");
}

void UserInputUi::on_pushButton_cancle_clicked() {
  is_ok = false;
  this->close();
}

void UserInputUi::on_pushButton_ok_clicked() {
  QString name = ui->comboBox_username->currentText();
  QString password = ui->lineEdit_password->text();

  if (!users.contains(name)) {
    ui->lineEdit_password->clear();
    ui->lineEdit_password->setPlaceholderText(" 用户不存在！！");
    return;
  } else if (users[name].password() == password) {
    is_ok = true;
    login_user = users[name];
    this->close();
  } else {
    ui->lineEdit_password->clear();
    ui->lineEdit_password->setPlaceholderText(" 输入密码错误！！");
  }
}

void UserInputUi::keyPressEvent(QKeyEvent* e) {
  if ((e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) { on_pushButton_ok_clicked(); }
}
